﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;

using ProjectBase.Core;

namespace ProjectBase.Data.NH
{
    /// <summary>
    ///     Base powerfull class for every Dao.
    ///     Every usefull method which can help with handling entity
    ///     should be placed here (e.g. GetAll())
    /// </summary>
    /// <typeparam name = "T">entity</typeparam>
    /// <typeparam name = "TID"></typeparam>
    /// <typeparam name = "TFilter"></typeparam>
    public abstract class BaseDao<T, TID, TFilter> : NHGenericDao<T, TID>, IEntityDao<T, TID, TFilter>
        where T : class, IPersistentObjectWithTypedId<TID>
        where TFilter : class, IFilter<TID>
    {
        /// <summary>
        ///     The Expression entity template,
        ///     which could be used for extension methods as the 'property marker'
        /// </summary>
        protected virtual T Entity { get; set; }

        #region IEntityDao<T,TID,TFilter> Members
        public abstract IEnumerable<T> Find(TFilter filter);
        #endregion

        /// <summary>
        ///     Uniqueness Check - Sometimes is useful to filter for existing entities
        ///     which are different from current entity (add or update)
        ///     Appends condition entity.ID &lt;&gt; exceptID
        /// </summary>
        /// <param name = "filter">Filtering parameters</param>
        /// <param name = "exceptID">The ID which should be excepted from the filtered list</param>
        /// <returns>The filtered list without entity of provided ID</returns>
        public virtual IEnumerable<T> Find(TID exceptID, TFilter filter)
        {
            Criteria.AddNotEqual(Constants.Common.ID, exceptID);
            return Find(filter);
        }

        #region Merge
        public virtual T ReAttach(T entity)
        {
            return Session.Merge(entity);
        }

        #endregion Merge
    }
}